﻿using System;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;
using HYView;

namespace YArchitech.Public
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYDesignNotes : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
				Document document = application.ActiveUIDocument.Document;
				CmdViewType types = CmdViewType.DrawingSheet;
                if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
				{
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
                    Autodesk.Revit.DB.View activeView = document.ActiveView;
					transaction = new Transaction(document, "设计说明");
					transaction.Start();
					DesignNoteForm designNoteForm = new DesignNoteForm();
					designNoteForm.ShowDialog();
					if (designNoteForm.DialogResult == DialogResult.OK)
					{
						bool flag;
						string strPath;
						if (designNoteForm.strPath == "")
						{
							string path = "";
							switch (designNoteForm.major)
							{
							case 0:
								path = "建筑设计说明.rvt";
								break;
							case 1:
								path = "给排水设计说明.rvt";
								break;
							case 2:
								path = "暖通设计说明.rvt";
								break;
							case 3:
								path = "电气设计说明.rvt";
								break;
							}
							flag = false;
							strPath = Path.Combine(Product.FamilyLocation, RevitVersion.Version, "DesignNote", path);
						}
						else
						{
							flag = true;
							strPath = designNoteForm.strPath;
						}
						DesignNoteHelper designNoteHelper = new DesignNoteHelper(application, strPath);
                        Autodesk.Revit.DB.View view;
						if (flag)
						{
							view = designNoteHelper.CustomDesignNote();
						}
						else
						{
							view = designNoteHelper.CopyDesignNote();
						}
						Viewport.Create(document, activeView.Id, view.Id, XYZ.Zero);
						document.Regenerate();
					}
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
            catch (Autodesk.Revit.Exceptions.ArgumentException ex)
			{
				if (ex.Message.Equals("导入文档为空"))
				{
					YJKMessageBox.Show("导入设计说明失败，请检查设计说明是否有误。");
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				else
				{
					if (!ex.Message.Equals("文件格式无效。"))
					{
						throw ex;
					}
					YJKMessageBox.Show("导入设计说明失败，请检查设计说明文件格式是否有误。");
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex2)
			{
				message = ex2.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
                    if (status == (TransactionStatus)1)
					{
						transaction.Commit();
					}
                    else if (status == (TransactionStatus)5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
